#include <cctype>
#include <cstdio>

typedef long long ll;
const int N = 3e5 + 5;
int n, tot, rt;

inline int read() {
  int ch = getchar(), res = 0;
  bool flag = false;
  while (!isdigit(ch)) {
    if (ch == '-') flag = true;
    ch = getchar();
  }
  while (isdigit(ch))
    res = (res << 1) + (res << 3) + (ch - '0'), ch = getchar();
  return flag ? -res : res;
}

struct Node {
  int ch[2], fa, val, siz;
  ll sum;
} tr[N];

inline bool iden(int x) { return tr[tr[x].fa].ch[1] == x; }

inline void pushup(int x) {
  static int ls, rs;
  if (x) {
    ls = tr[x].ch[0], rs = tr[x].ch[1];
    tr[x].sum = tr[x].val;
    tr[x].siz = 1;
    if (ls) tr[x].siz += tr[ls].siz, tr[x].sum += tr[ls].sum;
    if (rs) tr[x].siz += tr[rs].siz, tr[x].sum += tr[rs].sum;
  }
}

inline void connect(int x, int y, bool op) {
  if (x) tr[x].fa = y;
  if (y) tr[y].ch[op] = x;
}

inline void rotate(int x) {
  int fa = tr[x].fa, ffa = tr[fa].fa;
  bool n = iden(fa), xtofa = iden(x);
  connect(tr[x].ch[!xtofa], fa, xtofa);
  connect(fa, x, !xtofa);
  connect(x, ffa, n);
  pushup(fa);
  pushup(x);
}

inline void splay(int x) {
  for (int fa; (fa = tr[x].fa); rotate(x))
    if (tr[fa].fa) rotate(iden(fa) == iden(x) ? fa : x);
  rt = x;
}

inline void splay(int x, int tar) {
  for (int fa; (fa = tr[x].fa) != tar; rotate(x))
    if (tr[fa].fa != tar) rotate(iden(fa) == iden(x) ? fa : x);
}

inline int rank(int);

inline void insert(int cursor, int x) {
  if (!rt) {
    rt = ++tot;
    tr[rt].val = x;
    tr[rt].sum = x;
    tr[rt].siz = 1;
    tr[rt].ch[0] = tr[rt].ch[1] = 0;
    return;
  }
  if (cursor == 0) {
    int tmp = rt;
    while (tr[tmp].ch[0]) tmp = tr[tmp].ch[0];
    splay(tmp);
    tr[tmp].fa = ++tot;
    tr[tot].ch[1] = tmp;
    tr[tot].val = x;
    pushup(tot);
    rt = tot;
    return;
  }
  rank(cursor);
  ++tot;
  if (tr[rt].ch[1]) tr[tot].ch[1] = tr[rt].ch[1], tr[tr[rt].ch[1]].fa = tot;
  tr[rt].ch[1] = tot;
  tr[tot].val = x;
  tr[tot].fa = rt;
  pushup(tot), pushup(rt);
  splay(tot);
}

inline int rank(int x) {
  int cur = rt;
  while (cur) {
    if (x <= tr[tr[cur].ch[0]].siz) {
      cur = tr[cur].ch[0];
    } else if (x <= tr[tr[cur].ch[0]].siz + 1)
      break;
    else {
      x -= tr[tr[cur].ch[0]].siz + 1;
      cur = tr[cur].ch[1];
    }
  }
  if (cur) splay(cur);
  return cur;
}

inline void del(int x) {
  rank(x);
  if (!tr[rt].ch[1]) return rt = tr[rt].ch[0], tr[rt].fa = 0, void();
  else {
    int lef = tr[rt].ch[1], ori = rt;
    while (tr[lef].ch[0]) lef = tr[lef].ch[0];
    splay(lef);
    connect(tr[ori].ch[0], rt, 0);
    pushup(rt);
  }
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#else
  freopen("C.in", "r", stdin);
  freopen("C.out", "w", stdout);
#endif
#endif

  n = read();
  char opt[2];
  for (int x, l, r, p, cursor = 0, cnt = 0; n; --n) {
    scanf("%s", opt);
    switch (opt[0]) {
      case 'I':
        x = read();
        ++cnt;
        insert(cursor++, x);
        break;
      case 'D':
        --cnt;
        del(cursor--);
        break;
      case 'L':
        if (cursor != 0) cursor--;
        break;
      case 'R':
        if (cursor != cnt) cursor++;
        break;
      case 'Q':
        l = read(), r = read();
        l = rank(l);
        r = rank(r);
        if (l == r) {
          printf("%d\n", tr[r].val);
        } else {
          splay(l, r);
          printf("%lld\n", tr[tr[l].ch[1]].sum + tr[l].val + tr[r].val);
        }
        break;
      case 'C':
        p = read(), x = read();
        tr[rank(p)].val = x;
        pushup(rt);
    }
  }
}